package com.uzai.console.service.reportpanel.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.reportpanel.ReportPanelFansValueDto;
import com.uzai.console.dto.reportpanel.ReportPanelIdDto;
import com.uzai.console.dto.reportpanel.ReportPanelQuery;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.DeviceGroup;
import com.uzai.console.entity.ReportPanel;
import com.uzai.console.enums.ReportBanelFansValueShowFieldEnum;
import com.uzai.console.enums.ReportPanelTypeEnum;
import com.uzai.console.mapper.DeviceGroupMapper;
import com.uzai.console.mapper.ReportPanelMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.reportpanel.ReportPanelFansValueService;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.reportpanel.ReportPanelFansValueVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 报表面板
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class ReportPanelFansValueServiceImpl implements ReportPanelFansValueService {

    private Logger logger = LoggerFactory.getLogger(ReportPanelFansValueServiceImpl.class);

    @Autowired
    private ReportPanelMapper reportPanelMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;

    /**
     * 报表面板分页查询
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param reportPanelQuery
     * @return null
     */
    public Page<ReportPanelFansValueVo> findReportPanelFansValueByPage(ReportPanelQuery reportPanelQuery){

        //新建返回到页面的LIST对象
        List<ReportPanelFansValueVo> reportPanelVoList = new ArrayList<>();

        //返回到页面分页对象
        //执行分页查询对象
        Page<ReportPanel> page = new Page<>();
        reportPanelQuery.convert(page);

        Long merId = reportPanelQuery.getMerId();
        Integer type = reportPanelQuery.getType();

        //判断查询条件
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入运营商id");
        }

        if(type == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入报表面板类型");
        }

        List<ReportPanel> reportPanelList = reportPanelMapper.findByList(page, reportPanelQuery);

        if(reportPanelList != null && reportPanelList.size() > 0){
            for (ReportPanel reportPanel : reportPanelList){
                ReportPanelFansValueVo reportPanelFansValueVo = new ReportPanelFansValueVo();
                BeanUtils.copyProperties(reportPanel, reportPanelFansValueVo);

                //封装参数
                JSONObject jsonObject_para = JSONObject.parseObject(reportPanel.getParaData());
                if(jsonObject_para != null){
                    //封装查询开始时间
                    reportPanelFansValueVo.setQueryStartTime(jsonObject_para.getInteger("queryStartTime"));
                    reportPanelFansValueVo.setQueryEndTime(jsonObject_para.getInteger("queryEndTime"));
                    String deviceType = jsonObject_para.getString("deviceType");
                    //封装设备类型
                    reportPanelFansValueVo.setDeviceType(deviceType);
                    //封装查询机器人
                    List<Long> deviceUniqueIdList = JSONObject.parseArray(jsonObject_para.getString("deviceUniqueIdList"), Long.class);
                    if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
                        List<DeviceInfoVo> deviceInfoVoList = new ArrayList<>();
                        for (Long deviceUniqueId : deviceUniqueIdList) {
                            Device device = deviceService.findDeviceInfoByDeviceUniqueId(deviceUniqueId, deviceType, reportPanel.getMerId());
                            if(device != null){
                                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                                BeanUtils.copyProperties(device, deviceInfoVo);
                                deviceInfoVoList.add(deviceInfoVo);
                            }
                        }
                        //封装查询机器人
                        reportPanelFansValueVo.setDeviceInfoList(deviceInfoVoList);
                    }
                }

                //封装显示字段
                JSONObject jsonObject_showData = JSONObject.parseObject(reportPanel.getShowData());
                if(jsonObject_showData != null){
                    //封装查询机器人
                    List<String> showFieldList = JSONObject.parseArray(jsonObject_showData.getString("showFieldList"), String.class);
                    reportPanelFansValueVo.setShowFieldList(showFieldList);
                }
                reportPanelVoList.add(reportPanelFansValueVo);
            }
        }
        //返回到页面分页对象
        Page<ReportPanelFansValueVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(reportPanelVoList);

        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 粉丝价值报表面板根据id查询
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param reportPanelIdDto
     * @return null
     */
    public ReportPanelFansValueVo findReportPanelFansValueById(ReportPanelIdDto reportPanelIdDto){
        ReportPanelFansValueVo reportPanelFansValueVo = new ReportPanelFansValueVo();
        //查询报表面板
        ReportPanel reportPanel = reportPanelMapper.selectById(reportPanelIdDto.getId(), reportPanelIdDto.getMerId());
        if(reportPanel != null){
            BeanUtils.copyProperties(reportPanel, reportPanelFansValueVo);
            //封装参数
            JSONObject jsonObject_para = JSONObject.parseObject(reportPanel.getParaData());
            if(jsonObject_para != null){
                //封装查询开始时间
                reportPanelFansValueVo.setQueryStartTime(jsonObject_para.getInteger("queryStartTime"));
                reportPanelFansValueVo.setQueryEndTime(jsonObject_para.getInteger("queryEndTime"));
                String deviceType = jsonObject_para.getString("deviceType");
                //封装设备类型
                reportPanelFansValueVo.setDeviceType(deviceType);
                //封装查询机器人
                List<Long> deviceUniqueIdList = JSONObject.parseArray(jsonObject_para.getString("deviceUniqueIdList"), Long.class);
                if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
                    List<DeviceInfoVo> deviceInfoVoList = new ArrayList<>();
                    for (Long deviceUniqueId : deviceUniqueIdList) {
                        Device device = deviceService.findDeviceInfoByDeviceUniqueId(deviceUniqueId, deviceType, reportPanel.getMerId());
                        if(device != null){
                            DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                            BeanUtils.copyProperties(device, deviceInfoVo);
                            Long groupId = device.getGroupId();
                            DeviceGroup deviceGroup = deviceGroupMapper.selectById(groupId, device.getMerId());
                            if (Objects.nonNull(deviceGroup)) {
                                deviceInfoVo.setGroupName(deviceGroup.getName());
                            }
                            deviceInfoVoList.add(deviceInfoVo);
                        }
                    }
                    //封装查询机器人
                    reportPanelFansValueVo.setDeviceInfoList(deviceInfoVoList);
                }
            }
            //封装显示字段
            JSONObject jsonObject_showData = JSONObject.parseObject(reportPanel.getShowData());
            if(jsonObject_showData != null){
                //封装查询机器人
                List<String> showFieldList = JSONObject.parseArray(jsonObject_showData.getString("showFieldList"), String.class);
                reportPanelFansValueVo.setShowFieldList(showFieldList);
            }
        }
        return reportPanelFansValueVo;
    }


    /**
     * 报表面板添加
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param reportPanelFansValueDto
     * @return null
     */
    public void addReportPanelFansValue(ReportPanelFansValueDto reportPanelFansValueDto){

        Long merId = reportPanelFansValueDto.getMerId();
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入运营商id");
        }

        String deviceType = reportPanelFansValueDto.getDeviceType();
        if(Strings.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        List<Long> deviceUniqueIdList = reportPanelFansValueDto.getDeviceUniqueIdList();
        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }

        Integer queryStartTime = reportPanelFansValueDto.getQueryStartTime();
        if(queryStartTime == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择查询开始时间");
        }

        List<String> showFieldList = reportPanelFansValueDto.getShowFieldList();
        if(showFieldList == null || showFieldList.size() == 0){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择显示字段");
        }
        ReportPanel reportPanel = new ReportPanel();
        reportPanel.setId(IdWorker.getId());
        reportPanel.setMerId(merId);
        reportPanel.setType(ReportPanelTypeEnum.FANS_VALUE.getId());
        JSONObject para_data = new JSONObject();
        para_data.put("deviceType", deviceType);
        para_data.put("deviceUniqueIdList", deviceUniqueIdList);
        para_data.put("queryStartTime", queryStartTime);
        reportPanel.setParaData(para_data.toJSONString());
        JSONObject show_data = new JSONObject();
        show_data.put("showFieldList", showFieldList);
        reportPanel.setShowData(show_data.toJSONString());
        reportPanel.setRemark(reportPanelFansValueDto.getRemark());
        int now = DateUtil.getNowTime();
        reportPanel.setCreateTime(now);
        reportPanel.setUpdateTime(now);

        reportPanelMapper.insertSelective(reportPanel);

    }

    @Override
    public void editReportPanelFansValue(ReportPanelFansValueDto param) {

        ReportPanel exists = reportPanelMapper.selectById(param.getId(), param.getMerId());

        ReportPanel reportPanel = new ReportPanel();
        reportPanel.setId(param.getId());
        reportPanel.setMerId(param.getMerId());
        reportPanel.setType(ReportPanelTypeEnum.FANS_VALUE.getId());
        JSONObject para_data = JSON.parseObject(exists.getParaData());
        if (ObjectUtils.isNotEmpty(param.getDeviceUniqueIdList())) {
            para_data.put("deviceUniqueIdList", param.getDeviceUniqueIdList());
        }
        if (Objects.nonNull(param.getQueryEndTime())) {
            para_data.put("queryEndTime", param.getQueryEndTime());
        }
        if (Objects.nonNull(param.getQueryStartTime())) {
            para_data.put("queryStartTime", param.getQueryStartTime());
        }
        reportPanel.setParaData(para_data.toJSONString());
        if (ObjectUtils.isNotEmpty(param.getShowFieldList())) {
            JSONObject show_data = new JSONObject();
            show_data.put("showFieldList", param.getShowFieldList());
            reportPanel.setShowData(show_data.toJSONString());
        }

        reportPanel.setRemark(param.getRemark());
        int now = DateUtil.getNowTime();
        reportPanel.setCreateTime(now);
        reportPanel.setUpdateTime(now);

        reportPanelMapper.updateByIdSelective(reportPanel);
    }

    /**
     * 粉丝价值指标查询
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param reportPanelIdDto
     * @return null
     */
    public void deleteReportPanelFansValueById(ReportPanelIdDto reportPanelIdDto){

        if(reportPanelIdDto.getId() == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入主键id");
        }

        if(reportPanelIdDto.getMerId() == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "参数有误");
        }

        ReportPanel reportPanel = reportPanelMapper.selectById(reportPanelIdDto.getId(), reportPanelIdDto.getMerId());
        if(reportPanel == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该报表指标不存在或者已经删除");
        }

        //删除
        reportPanelMapper.deleteById(reportPanelIdDto.getId(), reportPanelIdDto.getMerId());

    }

    /**
     * 粉丝价值报表面板显示字段列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param
     * @return null
     */
    public HashMap<String,String> showFieldList(){
        HashMap<String,String> showFieldHashMap = new HashMap<>();
        ReportBanelFansValueShowFieldEnum[] reportBanelFansValueShowFieldEnumList =  ReportBanelFansValueShowFieldEnum.values();
        if(reportBanelFansValueShowFieldEnumList != null && reportBanelFansValueShowFieldEnumList.length > 0){
            for (ReportBanelFansValueShowFieldEnum reportBanelFansValueShowFieldEnum : reportBanelFansValueShowFieldEnumList) {
                showFieldHashMap.put(reportBanelFansValueShowFieldEnum.getKey(), reportBanelFansValueShowFieldEnum.getDesc());
            }
        }
        return showFieldHashMap;
    }

}
